﻿//-----------------------------------------------------------------------
// <copyright file="IMergeCellInternal.cs" company="LMT">
//     Copyright (c) by La Minh Thuận.
// </copyright>
// <author>thuanla1985@hotmail.com</author>
//-----------------------------------------------------------------------
namespace LMT.Forms
{
    using System;
    using System.Windows.Forms;
    using System.Drawing;

    /// <summary>
    /// Instance of every cell in MergeGridViewCheckBoxColumn
    /// </summary>
    public class MergeGridViewCheckBoxCell : DataGridViewCheckBoxCell, IMergeCell, IMergeCellInternal
    {
        /// <summary>
        /// variable whether this instance is vertical merge with next.
        /// </summary>
        private bool isVMergeN;

        /// <summary>
        /// variable whether this instance is vertical merge with previous.
        /// </summary>
        private bool isVMergeP;

        /// <summary>
        /// variable whether this instance is horizontal merge with next.
        /// </summary>
        private bool isHMergeN;

        /// <summary>
        /// variable whether this instance is horizontal merge with previous.
        /// </summary>
        private bool isHMergeP;

        /// <summary>
        /// 
        /// </summary>
        private SolidBrush bgColor;

        /// <summary>
        /// 
        /// </summary>
        private Pen penBorder;

        /// <summary>
        /// Initializes a new instance of the <see cref="MergeGridViewCheckBoxCell"/> class.
        /// </summary>
        public MergeGridViewCheckBoxCell() : base(false)
        {
        }

        /// <summary>
        /// </summary>
        /// <param name="graphics">The <see cref="T:System.Drawing.Graphics"/> used to paint the <see cref="T:System.Windows.Forms.DataGridViewCell"/>.</param>
        /// <param name="clipBounds">A <see cref="T:System.Drawing.Rectangle"/> that represents the area of the <see cref="T:System.Windows.Forms.DataGridView"/> that needs to be repainted.</param>
        /// <param name="cellBounds">A <see cref="T:System.Drawing.Rectangle"/> that contains the bounds of the <see cref="T:System.Windows.Forms.DataGridViewCell"/> that is being painted.</param>
        /// <param name="rowIndex">The row index of the cell that is being painted.</param>
        /// <param name="elementState"></param>
        /// <param name="value">The data of the <see cref="T:System.Windows.Forms.DataGridViewCell"/> that is being painted.</param>
        /// <param name="formattedValue">The formatted data of the <see cref="T:System.Windows.Forms.DataGridViewCell"/> that is being painted.</param>
        /// <param name="errorText">An error message that is associated with the cell.</param>
        /// <param name="cellStyle">A <see cref="T:System.Windows.Forms.DataGridViewCellStyle"/> that contains formatting and style information about the cell.</param>
        /// <param name="advancedBorderStyle">A <see cref="T:System.Windows.Forms.DataGridViewAdvancedBorderStyle"/> that contains border styles for the cell that is being painted.</param>
        /// <param name="paintParts">A bitwise combination of the <see cref="T:System.Windows.Forms.DataGridViewPaintParts"/> values that specifies which parts of the cell need to be painted.</param>
        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            if (this.ReadOnly)
            {
                if (value != null)
                {
                    bool result = false;
                    if (bool.TryParse(value.ToString(), out result))
                    {
                        if (bgColor == null)
                        {
                            this.bgColor = new SolidBrush(this.OwningRow.DefaultCellStyle.BackColor);
                        }
                        if (penBorder == null)
                        {
                            this.penBorder = new Pen(this.DataGridView.GridColor, 1);
                        }

                        this.bgColor.Color = this.OwningRow.Selected ? this.DataGridView.DefaultCellStyle.SelectionBackColor : this.DataGridView.DefaultCellStyle.BackColor;
                        graphics.FillRectangle(this.bgColor, cellBounds);
                        Rectangle bound = cellBounds;
                        bound.Offset(-1,-1);
                        graphics.DrawRectangle(this.penBorder, bound);

                        if (result)
                        {
                            RectangleF rect = RectangleF.Empty;
                            rect.Size = new SizeF(16, 16);
                            rect.X = cellBounds.Location.X + ( cellBounds.Width / 2 ) - 8;
                            rect.Y = cellBounds.Location.Y + 3;
                            graphics.DrawImage(global::LMT.Forms.Properties.Resource.ok, rect);
                        }
                    }
                    else
                    {
                        base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
                    }
                }
                else
                {
                    base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
                }
            }
            else
            {
                base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
            }
        }

        #region IMergeCell Members

        /// <summary>
        /// Gets a value indicating whether this instance is vertical merge.
        /// </summary>
        /// <value>
        ///   <c>true</c> if this instance is vertical merge; otherwise, <c>false</c>.
        /// </value>
        public bool IsVMergeNext
        {
            get 
            { 
                return this.isVMergeN; 
            }
        }

        /// <summary>
        /// Gets a value indicating whether this instance is vertical merge previous.
        /// </summary>
        /// <value>
        ///   <c>true</c> if this instance is vertical merge previous; otherwise, <c>false</c>.
        /// </value>
        public bool IsVMergePrevious
        {
            get 
            { 
                return this.isVMergeP; 
            }
        }

        /// <summary>
        /// Gets a value indicating whether this instance is horizontal merge next.
        /// </summary>
        /// <value>
        ///   <c>true</c> if this instance is horizontal merge; otherwise, <c>false</c>.
        /// </value>
        public bool IsHMergeNext
        {
            get 
            { 
                return this.isHMergeN; 
            }
        }

        /// <summary>
        /// Gets a value indicating whether this instance is horizontal merge previous.
        /// </summary>
        /// <value>
        ///   <c>true</c> if this instance is H merge previous; otherwise, <c>false</c>.
        /// </value>
        public bool IsHMergePrevious
        {
            get 
            { 
                return this.isHMergeP; 
            }
        }

        #endregion

        #region IMergeCellSetter Members

        /// <summary>
        /// Gets or sets a value indicating whether this instance is vertical merge.
        /// </summary>
        /// <value>
        ///   <c>true</c> if this instance is vertical merge; otherwise, <c>false</c>.
        /// </value>
        bool IMergeCellInternal.IsVMergeNext
        {
            get { return this.isVMergeN; }
            set { this.isVMergeN = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is vertical merge previous.
        /// </summary>
        /// <value>
        ///   <c>true</c> if this instance is vertical merge previous; otherwise, <c>false</c>.
        /// </value>
        bool IMergeCellInternal.IsVMergePrevious
        {
            get { return this.isVMergeP; }
            set { this.isVMergeP = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is horizontal merge next.
        /// </summary>
        /// <value>
        ///   <c>true</c> if this instance is horizontal merge; otherwise, <c>false</c>.
        /// </value>
        bool IMergeCellInternal.IsHMergeNext
        {
            get { return this.isHMergeN; }
            set { this.isHMergeN = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is horizontal merge previous.
        /// </summary>
        /// <value>
        ///   <c>true</c> if this instance is H merge previous; otherwise, <c>false</c>.
        /// </value>
        bool IMergeCellInternal.IsHMergePrevious
        {
            get { return this.isHMergeP; }
            set { this.isHMergeP = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is analyze merge.
        /// </summary>
        /// <value>
        ///  <c>true</c> if this instance is analyze merge; otherwise, <c>false</c>.
        /// </value>
        bool IMergeCellInternal.IsAnalyzeMerge { get; set; }

        #endregion
    }
}
